package com.ay.selfservicemachine.permission;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;

import com.ay.selfservicemachine.databinding.DialogPermissionSettingBinding;
import com.ay.selfservicemachine.dialog.BaseDialog;


public class PermissionSettingDialog extends BaseDialog<DialogPermissionSettingBinding> implements View.OnClickListener {


    PermissionListener permissionListener;
    private String msg;

    @Override
    protected DialogPermissionSettingBinding viewBinding() {
        return DialogPermissionSettingBinding.inflate(getLayoutInflater());
    }


    public PermissionSettingDialog(@NonNull Context context, PermissionListener permissionListener,String msg) {
        super(context);
        this.permissionListener = permissionListener;
        this.msg = msg;
        setCancelable(false);
        setBorderExtension();
    }


    @Override
    protected void initView() {
        super.initView();
        root.tvPermissionCancel.setOnClickListener(this);
        root.tvPermissionVerify.setOnClickListener(this);
        if(!TextUtils.isEmpty(msg)){
            root.tvPermissionContent.setText(msg);
        }
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == root.tvPermissionCancel.getId()) {
            permissionListener.onfailure();
            dismiss();
        } else if (v.getId() == root.tvPermissionVerify.getId()) {
            Intent intent = new Intent();
            intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            Uri uri = Uri.fromParts("package", getContext().getPackageName(), null);
            intent.setData(uri);
            getContext().startActivity(intent);
            permissionListener.onfailure();
            dismiss();
        }
    }
}
